<template>
    <el-container class="layout-container" :class="{ 'dark-mode': isDarkMode }">
        <!-- 顶部导航栏 -->
        <el-header>
            <div class="nav-bar">
                <el-menu :default-active="activeIndex" class="el-menu-demo" mode="horizontal" @select="handleSelect">
                    <el-menu-item index="/home">
                        <el-icon>
                            <Promotion />
                        </el-icon>
                        <span>首页</span>
                    </el-menu-item>
                    <el-menu-item index="/article/category">
                        <el-icon>
                            <Management />
                        </el-icon>
                        <span>文章分类</span>
                    </el-menu-item>
                    <el-menu-item index="/article/manage">
                        <el-icon>
                            <Promotion />
                        </el-icon>
                        <span>文章管理</span>
                    </el-menu-item>
                    <el-sub-menu index="/user">
                        <template #title>
                            <el-icon>
                                <UserFilled />
                            </el-icon>
                            <span>个人中心</span>
                        </template>
                        <el-menu-item index="/user/info">
                            <el-icon>
                                <User />
                            </el-icon>
                            <span>基本资料</span>
                        </el-menu-item>
                        <el-menu-item index="/user/avatar">
                            <el-icon>
                                <Crop />
                            </el-icon>
                            <span>更换头像</span>
                        </el-menu-item>
                        <el-menu-item index="/user/resetPassword">
                            <el-icon>
                                <EditPen />
                            </el-icon>
                            <span>重置密码</span>
                        </el-menu-item>
                    </el-sub-menu>
                    <el-menu-item>
                        <el-icon>
                            <Promotion />
                        </el-icon>
                        <a class="no-style-link" target="_blank"
                            href="https://blog.csdn.net/WDJ54068923?spm=1000.2115.3001.5343"><span>联系我</span></a>
                    </el-menu-item>
                    <el-menu-item>
                        <el-switch v-model="isDarkMode" active-text="夜间模式" inactive-text="日间模式"
                            @change="toggleDarkMode" />
                    </el-menu-item>
                </el-menu>
                <div class="search-box">
                    <el-input placeholder="请输入搜索内容" v-model="searchQuery" class="search-input">
                        <template #append>
                            <el-button type="primary" @click="handleSearch" class="search-button">
                                <svg t="1732890146755" class="icon" viewBox="0 0 1024 1024" version="1.1"
                                    xmlns="http://www.w3.org/2000/svg" p-id="3138" width="200" height="200">
                                    <path
                                        d="M446.112323 177.545051c137.567677 0.219798 252.612525 104.59798 266.162424 241.493333 13.562828 136.895354-78.778182 261.818182-213.617777 289.008485-134.852525 27.203232-268.386263-52.156768-308.945455-183.608889s25.018182-272.252121 151.738182-325.779394A267.235556 267.235556 0 0 1 446.112323 177.545051m0-62.060607c-182.794343 0-330.989899 148.195556-330.989899 330.989899s148.195556 330.989899 330.989899 330.989899 330.989899-148.195556 330.989899-330.989899-148.195556-330.989899-330.989899-330.989899z m431.321212 793.341415a30.849293 30.849293 0 0 1-21.94101-9.102223l-157.220202-157.220202c-11.752727-12.179394-11.584646-31.534545 0.37495-43.50707 11.972525-11.972525 31.327677-12.140606 43.494141-0.37495l157.220202 157.220202a31.036768 31.036768 0 0 1 6.723232 33.810101 31.004444 31.004444 0 0 1-28.651313 19.174142z m0 0"
                                        p-id="3139"></path>
                                </svg>
                            </el-button>
                        </template>
                    </el-input>
                </div>
                <el-dropdown placement="bottom-end" @command="handleCommand">
                    <span class="el-dropdown__box">
                        <el-avatar :src="userInfoStore.info.userPic ? userInfoStore.info.userPic : avatar" />
                        <el-icon>
                            <CaretBottom />
                        </el-icon>
                    </span>
                    <template #dropdown>
                        <el-dropdown-menu>
                            <el-dropdown-item command="info" :icon="User">基本资料</el-dropdown-item>
                            <el-dropdown-item command="avatar" :icon="Crop">更换头像</el-dropdown-item>
                            <el-dropdown-item command="resetPassword" :icon="EditPen">重置密码</el-dropdown-item>
                            <el-dropdown-item command="logout" :icon="SwitchButton">退出登录</el-dropdown-item>
                        </el-dropdown-menu>
                    </template>
                </el-dropdown>
            </div>
        </el-header>
        <!-- 中间区域 -->
        <el-main>
            <router-view></router-view>
        </el-main>
        <!-- 底部区域 -->
        <el-footer>大事件 ©2023</el-footer>
    </el-container>
</template>

<script setup>
import {
    Management,
    Promotion,
    UserFilled,
    User,
    Crop,
    EditPen,
    SwitchButton,
    CaretBottom
} from '@element-plus/icons-vue'
import avatar from '@/assets/default.png'

// 导入 ref
import { ref } from 'vue';

// 导入接口函数
import { userInfoGetService } from '@/api/user.js'
// 导入 pinia
import { useUserInfoStore } from '@/stores/userInfo.js'
const userInfoStore = useUserInfoStore();

// 获取个人信息
const getUserInf = async () => {
    let result = await userInfoGetService();
    // 存储 pinia
    userInfoStore.info = result.data;
}
getUserInf()

// dropDown 条目被点击后，回调的函数
import { useRouter } from 'vue-router'
import { useTokenStore } from '@/stores/token';
import { ElMessage } from 'element-plus';

const router = useRouter()
const searchQuery = ref('')
const activeIndex = ref('/home') // 当前选中的菜单项
const isDarkMode = ref(false) // 夜间模式状态

const handleCommand = (command) => {
    if (command === 'logout') {
        // 退出登录
        const tokenStore = useTokenStore();
        tokenStore.removeToken();
        ElMessage.success('退出登录')
        router.push('/login')
    } else {
        // 路由
        router.push('/user/' + command)
    }
}

const handleSelect = (key, keyPath) => {
    router.push(key)
}

const handleSearch = () => {
    console.log('搜索内容:', searchQuery.value)
    // 在这里处理搜索逻辑
}

const toggleDarkMode = () => {
    // 切换夜间模式
    document.documentElement.classList.toggle('dark-mode', isDarkMode.value);
}
</script>

<style lang="scss" scoped>
.layout-container {
    height: 100vh;
    display: flex;
    flex-direction: column;

    &.dark-mode {
        background-color: #1e1e1e;

        .el-header {
            background-color: #333;

            .nav-bar {
                .el-menu {
                    background-color: #333;
                    border-bottom: none;

                    .el-menu-item,
                    .el-sub-menu .el-sub-menu__title {
                        color: #fff;
                    }

                    .--el-menu-text-color {
                        color: #ffffff !important;
                    }
                }

                .search-box {
                    .search-input {
                        .el-input__inner {
                            background-color: #444;
                            color: #fff;
                        }

                        .el-input-group__append {
                            .search-button {
                                background-color: #444;
                                color: #fff;
                            }
                        }
                    }
                }

                .el-dropdown__box {
                    .el-avatar {
                        border: 2px solid #fff;
                    }

                    .el-icon {
                        color: #fff;
                    }
                }
            }

            .el-sub-menu .el-menu {
                background-color: #333;
            }
        }

        .el-main {
            background-color: #222;
            color: #fff;
        }

        .el-footer {
            background-color: #333;
            color: #fff;
        }
    }

    .el-header {
        background-color: #fff;
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 0 20px;

        .nav-bar {
            display: flex;
            align-items: center;
            justify-content: space-between;
            width: 100%;

            .el-menu {
                border-bottom: none;
            }

            .search-box {
                flex: 1;
                max-width: 400px;
                margin: 0 20px;

                .search-input {
                    display: flex;
                    align-items: center;
                    border-radius: 20px;
                    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
                    transition: all 0.3s ease;

                    &:focus-within {
                        box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
                    }

                    .el-input__inner {
                        border: none;
                        border-radius: 20px;
                        padding-left: 20px;
                        background-color: #f5f5f5;
                        color: #333;
                        font-size: 14px;
                        flex: 1;
                    }

                    .el-input-group__append {
                        background-color: transparent;
                        border: none;
                        margin-left: 10px;
                        margin-right: 5px;
                        padding: 0 15px;

                        .search-button {
                            font-weight: bold;
                            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
                            transition: all 0.3s ease;
                            padding: 0 15px;
                            /* 调整按钮内边距 */

                            svg {
                                width: 1em;
                                /* 使图标大小与文字一致 */
                                height: 1em;
                                /* 使图标大小与文字一致 */
                                vertical-align: middle;
                                /* 垂直居中对齐 */
                            }

                            &:hover {
                                box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3);
                            }
                        }
                    }
                }
            }

            .el-dropdown__box {
                display: flex;
                align-items: center;

                .el-icon {
                    color: #999;
                    margin-left: 10px;
                }

                &:active,
                &:focus {
                    outline: none;
                }
            }
        }
    }

    .el-footer {
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 14px;
        color: #666;
    }

    .el-main {
        flex: 1;
        /* 占据剩余空间 */
        padding-top: 0px;
        width: 1400px;
        /* 固定宽度 */
        margin: 0 auto;
        /* 居中显示 */
        overflow-y: scroll;
        /* 确保有垂直滚动条 */

        &::-webkit-scrollbar {
            display: none;
            /* 隐藏滚动条 */
        }

        scrollbar-width: none;
        /* Firefox */
    }
}

.no-style-link {
    text-decoration: none;
    color: inherit;
    /* 继承父元素的颜色 */
    cursor: pointer;

    &:visited {
        color: inherit;
        /* 访问过的链接颜色也继承父元素的颜色 */
    }

    &:hover {
        text-decoration: none;
        /* 鼠标悬停时无下划线 */
    }

    &:active {
        text-decoration: none;
        /* 鼠标点击时无下划线 */
    }
}
</style>